package it.espsoft.easysuperenalotto.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import it.espsoft.easysuperenalotto.bean.GiocataBean;
import it.espsoft.easysuperenalotto.db.DB;
import it.espsoft.easysuperenalotto.db.DataBaseCreationHelper;
import it.espsoft.easysuperenalotto.db.TableNames;

public class GiocataDAO implements TableNames{

	public static void insert(GiocataBean gb) {
		ContentValues cv = new ContentValues();
		cv.put("primo", gb.getPrimo());
		cv.put("secondo", gb.getSecondo());
		cv.put("terzo", gb.getTerzo());
		cv.put("quarto", gb.getQuarto());
		cv.put("quinto", gb.getQuinto());
		cv.put("sesto", gb.getSesto());
		cv.put("superstar", gb.getSuperstar());
		DB.getDataBase().insert(GIOCATE_TABLE_NAME, null, cv);
	}
	
	public static List<GiocataBean> getGiocate(){
		Cursor cursor = DB.getDataBase().query(DataBaseCreationHelper.GIOCATE_TABLE_NAME, null,null,null,null,null,null);
		return giocateFromCursor(cursor);
	}
	
	private static List<GiocataBean> giocateFromCursor(Cursor cursor){
		List<GiocataBean> result = new ArrayList<GiocataBean>();
		int idClmn = cursor.getColumnIndex("id");
		int primoClmn = cursor.getColumnIndex("primo"); 
        int secondoClmn = cursor.getColumnIndex("secondo"); 
        int terzoClmn = cursor.getColumnIndex("terzo"); 
        int quartoClmn = cursor.getColumnIndex("quarto"); 
        int quintoClmn = cursor.getColumnIndex("quinto"); 
        int sestoClmn = cursor.getColumnIndex("sesto"); 
        int superstarClmn = cursor.getColumnIndex("superstar");
        
        if(cursor.moveToFirst()){
        	do{
        		GiocataBean gb = new GiocataBean();
        		gb.setId(cursor.getInt(idClmn));
        		gb.setPrimo(cursor.getString(primoClmn));
        		gb.setSecondo(cursor.getString(secondoClmn));
        		gb.setTerzo(cursor.getString(terzoClmn));
        		gb.setQuarto(cursor.getString(quartoClmn));
        		gb.setQuinto(cursor.getString(quintoClmn));
        		gb.setSesto(cursor.getString(sestoClmn));
        		gb.setSuperstar(cursor.getString(superstarClmn));
        		result.add(gb);
        	}while(cursor.moveToNext());
        }
        return result;
	}
	
	public static GiocataBean getGiocata(int id){
		Cursor cursor = DB.getDataBase().query(GIOCATE_TABLE_NAME, null, "id=?", new String [] {""+id},null,null,null);
		List<GiocataBean> result = giocateFromCursor(cursor);
		if(result!=null && result.size()>0) return result.get(0); else return null;
	}
	
	public static int deleteGiocata(int id){
		return DB.getDataBase().delete(GIOCATE_TABLE_NAME, "id=?", new String[] {""+id});
	}

	public static void update(GiocataBean gb) {
		ContentValues cv = new ContentValues();
		cv.put("primo", gb.getPrimo());
		cv.put("secondo", gb.getSecondo());
		cv.put("terzo", gb.getTerzo());
		cv.put("quarto", gb.getQuarto());
		cv.put("quinto", gb.getQuinto());
		cv.put("sesto", gb.getSesto());
		cv.put("superstar", gb.getSuperstar());
		DB.getDataBase().update(GIOCATE_TABLE_NAME, cv, "id=?", new String[] {""+gb.getId()});
	}
}
